using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Recruiters_register : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
        WebConfigurationManager.ConnectionStrings[
        "RecruitVietnam"].ConnectionString;
        if (Session["rcuserid"] == null)
        {
            NotLogin();
        }
        else
        {
            Logged();
        }
    }
    void NotLogin()
    {
        lblHeader.Text = "New Registration";
        this.Title = "::New Registration";
        literalMessage.Visible = true;
        trTermofService.Visible = true;
        trAgree.Visible = true;
        txtEmail.Enabled = true;
        btnSave.Text = "I Agree";

        Page.ClientScript.RegisterClientScriptBlock(
        this.GetType(), "ClientScriptFunctionAgree",
        ClientScriptFunctionAgree(), true);
        btnSave.Attributes.Add("onclick",
        "return validateAgree();");
        trPassword.Visible = true;
        trRetype.Visible = true;
        btnNotAgree.Visible = true;
    }
    void Logged()
    {
        this.Title = "::Update Login Information";
        literalMessage.Visible = false;
        trTermofService.Visible = false;
        trAgree.Visible = false;
        lblHeader.Text = "Update Login Information";
        txtEmail.Enabled = false;
        txtEmail.Text = Session["rcemail"].ToString();
        btnSave.Text = "Update";
        trPassword.Visible = false;
        trRetype.Visible = false;
        btnNotAgree.Visible = false;
        if (!IsPostBack)
            EditAccount();
    }
    void EditAccount()
    {
        SqlServerProvider cls = new
        SqlServerProvider(connectionString);
        cls.CommandText = "select CompanyName,"
        + "EngLishName, AccountType, "+
        "Newsletter, Promotion,ResumeAlert," +
        " Activate from tblRCAccounts" +
        " where RecruiterID=@RecruiterID";
        cls.ParameterCollection =
        new string[1] { "@RecruiterID" };
        cls.ValueCollection = new
        string[1] { Session["rcuserid"].ToString() };
        DataRow dataRow =
        cls.GetDataRow();
        txtCompanyName.Text =
        dataRow[0].ToString();
        txtEnglishName.Text =
        dataRow[1].ToString();
        rblType.SelectedValue = 
        (dataRow[2].ToString() == "True") ? "1" : "0"; 
        chkNewLetter.Checked =
        (dataRow[3].ToString() == "True");
        chkPromotinAlert.Checked =
        (dataRow[4].ToString() == "True");
        chkResumeAlert.Checked =
        (dataRow[5].ToString() == "True");
        
    }

    string ClientScriptFunctionAgree()
    {
        string strScript = "";
        strScript =
        "function validateAgree()\n" +
        "{\n if(!document.getElementById('"
        + chkAgree.ID + "').checked){alert('"
        + "Please check Agree checkbox " +
        "if you agree our policy');" +
        "return false;} return true;" +
        " \n}\n";
        return strScript;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (txtEmail.Text != "")
        {
            RecruitVietnam cls = new RecruitVietnam();
            cls.ConnectionString = connectionString;
            if (Session["rcuserid"] == null)
            {
                int userID = cls.AddNewRCAccount(
                txtEmail.Text,
                txtPassword.Text,
                txtCompanyName.Text,
                txtEnglishName.Text,
                rblType.SelectedValue,
                (chkNewLetter.Checked) ? "1" : "0",
                (chkPromotinAlert.Checked) ? "1" : "0",
                (chkResumeAlert.Checked) ? "1" : "0");
                if (cls.ErrorCode == 2627)
                {
                    literalError.Text =
                    "Sorry! RCV database show that " +
                    "someone has registered a " +
                    "recruiter account with this e-mail " +
                    "address. If you have previously " +
                    "registered under this e-mail " +
                    "address and forgotten your " +
                    "password, you can request for " +
                    "your password <a href=\"" +
                    "forgotpassword.aspx\">here</a>";
                }
                else if (cls.ErrorMessage != "")
                {
                    literalError.Text =
                    "Error: " + cls.ErrorMessage;
                }
                else
                {
                    MailProvider mailProvider 
                    = new MailProvider();
                    mailProvider.SendMail(txtEmail.Text,
                    "New Registration", "Thanks you for" +
                    " your registration at "+
                    " RecruitVietnam.com."+
                    " We will consider your account "+
                    "and send back our approal later.");
                    
                    Session.Add("rcuserid", userID);
                    Session.Add("rcemail", txtEmail.Text);
                    Session.Add("rccompanyname",
                    txtCompanyName.Text + " " 
                    + txtEnglishName.Text);
                    Session.Add("rcjobfree", "0");
                    Session.Add("rcactivate", "0");
                    literalError.Text = "Your account has been created and you are now signed in."; 
                }
            }
            else
            {
                literalError.Text = "Updated successfull";
                cls.UpdateRCAccount(
                Session["rcuserid"].ToString(),
                txtEmail.Text,
                txtCompanyName.Text,
                txtEnglishName.Text,
                rblType.SelectedValue,
                (chkNewLetter.Checked) ? "1" : "0",
                (chkPromotinAlert.Checked) ? "1" : "0",
                (chkResumeAlert.Checked) ? "1" : "0");
                Session["rccompanyname"] = txtCompanyName.Text
                + " " + txtEnglishName.Text;
            }
            Response.Redirect("profile.aspx");
        }
        else
        {
            literalError.Text =
            "Please check on Agree before submit";
        }
    }
    
}
